package pe.com.tma.bean;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.CroppedImage;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

import pe.com.tma.model.Cuentasab;
import pe.com.tma.model.Deposito;
import pe.com.tma.model.Usuario;
import pe.com.tma.service.CuentaService;
import pe.com.tma.service.VoucherService;

@ManagedBean(name="voucherBean")
@SessionScoped
public class VoucherBean implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private Deposito deposito;
	private List<SelectItem> cuentas;
	private Usuario user;
	private StreamedContent imagem  = new DefaultStreamedContent();
	private static CuentaService srvCtas;
	private static VoucherService srvVoucher;
	
	private String imagemTemporaria;
	private CroppedImage croppedImage;
	private boolean exibeBotao = false;
	
	public VoucherBean(){
		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(true);
		srvCtas = new CuentaService();
		
		user = (Usuario) session.getAttribute("user");
		cuentas = srvCtas.CboCuentas();
		//System.out.println("Size : " + cuentas.size());
		
		this.setExibeBotao(false);
		deposito = new Deposito();
		
	}

	public void grabar(ActionEvent actionEvent){
		FacesContext fCtx = FacesContext.getCurrentInstance();
		srvVoucher = new VoucherService();
		String txt;
		
		int i = srvVoucher.GrabarVoucher(deposito);
		
		if (i==0){
			txt = "Registro Exisotoso";
		}else{
			txt = "Error en el Registro";
		}
		FacesMessage msg = new FacesMessage("Ingresi Exisotoso.");	
		fCtx.addMessage( null, msg);  
	}
	
	
	public void handleFileUpload(FileUploadEvent event) {
		UploadedFile file ;
		FacesContext fCtx = FacesContext.getCurrentInstance();
		
		try {
			file = event.getFile();
			
			//InputStream tempo = file.getInputstream();
			//byte[] fotoBinary = new byte[tempo.available()];
			//tempo.read(fotoBinary);
			//tempo.close();
			
			byte[] fotoBinary = new byte[file.getInputstream().available()];
			deposito.setImagen(fotoBinary);
			deposito.setUsuario(user);
			deposito.setIdDeposito(1);
			//Mostrar imagen en el Form
			this.setImagem(new DefaultStreamedContent(file.getInputstream()));
			//-------------------------------
			setExibeBotao(true);
			System.out.println("handleFileUpload ");
			FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
			FacesContext.getCurrentInstance().addMessage( null, msg);  
						
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	
	public void criaArquivo(byte[] bytes, String arquivo) {
      FileOutputStream fos;
      try {
         fos = new FileOutputStream(arquivo);
         fos.write(bytes);
         fos.close();
      } catch (FileNotFoundException ex) {
//         Logger.getLogger(IndexManagedBean.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
//         Logger.getLogger(IndexManagedBean.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
	
//	public void crop() {
//	      setImagem(new DefaultStreamedContent(new ByteArrayInputStream(croppedImage.getBytes())));
//	   }
	
	public Deposito getDeposito() {
		return deposito;
	}

	public void setDeposito(Deposito deposito) {
		this.deposito = deposito;
	}

	public List<SelectItem> getCuentas() {
		return cuentas;
	}

	public void setCuentas(List<SelectItem> cuentas) {
		this.cuentas = cuentas;
	}

	public StreamedContent getImagem() {
		return imagem;
	}

	public void setImagem(StreamedContent imagem) {
		this.imagem = imagem;
	}

	public CroppedImage getCroppedImage() {
		return croppedImage;
	}

	public void setCroppedImage(CroppedImage croppedImage) {
		this.croppedImage = croppedImage;
	}

	public boolean isExibeBotao() {
		return exibeBotao;
	}

	public void setExibeBotao(boolean exibeBotao) {
		this.exibeBotao = exibeBotao;
	}

	public String getImagemTemporaria() {
		return imagemTemporaria;
	}

	public void setImagemTemporaria(String imagemTemporaria) {
		this.imagemTemporaria = imagemTemporaria;
	}

	
}
